﻿@using DHQGHN.PM0
@Styles.Render("~/Content/css")
<script type="text/javascript">
    $(document).ready(function () {
        $('#side-menu').css('display', '');
    });
</script>

@helper GenerateAdminMenu()
{
    if (string.IsNullOrEmpty(User.Identity.Name))
    {

        GlobalInfo globalInfo = new GlobalInfo();

        var allMenu = DHQGHN.PM0.Common.Helpers.GetMenuHelper.GetAllMenu();
        var rootMenu = allMenu.Where(o => o.MenuParentId == 0).ToList();
        
        if (rootMenu.Count > 0 )
        {
            
            int i = 0;
            <ul id="side-menu" class="nav" style="display: none">
                
                @foreach (var menu in rootMenu)
                {
                    var listmenu = allMenu.Where(o => o.MenuParentId == menu.MenuId).ToList();
                    if (listmenu.Count > 0)
                    {
                        var cssClass = "menu-parent menu-root";
                        i++;
                        <li class="@cssClass">
                            <a href="@menu.MenuUrl">
                                <i class="@menu.MenuClass"></i>
                                @menu.MenuName
                                <span class="fa arrow"></span>
                            </a>
                            @GenerateNote(menu.MenuId, menu.MenuName, 0, null, listmenu, allMenu)
                        </li>
                    }
                }
            </ul>
        }
    }
}

@helper GenerateNote(int parrentId, string departmentName, int level, List<string> actionCode, List<DHQGHN.DAL.Models.AdminMenuModel> listMenu, List<DHQGHN.DAL.Models.AdminMenuModel> allMenu)
{
    level++;
    if (listMenu.Count > 0)
    {
        var classLevelul = "";
        if (level == 1)
        {
            classLevelul = "nav nav-second-level";
        }
        else if (level == 2)
        {
            classLevelul = "nav nav-third-level";
        }
        <ul class="@classLevelul">
            @foreach (var menu in listMenu)
            {
                var listChild = allMenu.Where(o => o.MenuParentId == menu.MenuId).ToList();
                var classLevel = "";
                if (level == 1)
                {
                    classLevel = "li-nav-second-level";
                }
                else if (level == 2)
                {
                    classLevel = "li-nav-third-level";
                }

                if (HttpContext.Current.Request.Url.AbsoluteUri.Contains(menu.MenuUrl))
                {
                    classLevel += " active";
                }
                if (menu.MenuUrl.Equals("#"))
                {
                    if (listChild.Count > 0)
                    {
                        <li class="@classLevel">
                            <a href="@menu.MenuUrl">
                                <i class="@menu.MenuClass"></i>
                                <span style="color: black;">@menu.MenuName</span>
                                <span class="fa arrow"></span>
                            </a>
                            @GenerateNote(menu.MenuId, menu.MenuName, level,null, listChild, allMenu)
                        </li>
                    }
                }
                else
                {
                    <li class="@classLevel">
                        <a href="@menu.MenuUrl">
                            <i class="@menu.MenuName"></i>
                            <span style="color: black;">@menu.MenuName</span>
                        </a>
                    </li>
                }
            }
        </ul>
    }
}

<div id="menu_left_content" class="navbar-default navbar-static-side" style="width: 200px" role="navigation">
    <div class="sidebar-collapse">
        @GenerateAdminMenu()
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var $rootmenu = $("#side-menu");
        var $allLi = $rootmenu.find('li');
        _.each($allLi, function (itemli) {
            var allUl = $(itemli).find('ul');
            if (allUl.length > 0) {
                var hasli = allUl.find('li').length > 0;
                if (!hasli) {
                    $(itemli).remove();
                }
            }
        });
    });
</script>
